<?php
class DictAction extends CommonAction{

	public function index(){
		// 		$Dict	= M("Dict");
		// 		$list	=	$Dict->limit(5)->order('id desc')->findAll();
		// 		$this->assign('list',$list);
		// 		$this->display();

		//自定义
		$Dict=M('Dict');

		$condForm['code'] = $_POST['code'];
		$condForm['code_name'] = $_POST["code_name"];

		if(!empty($condForm['code'])){
			$condition['code'] = array('like',makeSqlLikeValue($condForm['code']));
		}

		if(!empty($condForm['code_name'])){
			$condition['code_name'] = array('like',makeSqlLikeValue($condForm['code_name']));
		}
		import("@.ORG.Page"); //导入分页类
		$count = $Dict->count();    //计算总数
		$p = new Page ( $count, self::DEFULT_LIMIT_SIZE );
		if(isset($condition)){
			$list=$Dict->where($condition)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		}else{
			$list=$Dict->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		}
		$p->setConfig('header','篇记录');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$page = $p->show ();
		$this->assign("condForm",$condForm);
		$this->assign( "page", $page );
		$this->assign ( "list", $list );
		$this->display();
	}

	public function add(){
		$Dict = M("Dict");
		$parents = $Dict->findAll();
		$this->assign("parents", $parents);
		$this-> display();
	}

	public function insert(){
		$Dict = D("Dict");
		if($vo = $Dict->create()) {
			$list=$Dict->add();
			if($list!==false){
				$this->success('数据保存成功！');
			}else{
				$this->error('数据写入错误！');
			}
		}else{
			$this->error($Form->getError());
		}
	}

	// 编辑数据
	public function edit() {
		if(!empty($_GET['id'])) {
			$Form	=	D("Dict");
			$vo	=	$Form->relation(true)->getById($_GET['id']);
			if($vo) {
				$this->assign('vo',$vo);
				$this->display();
			}else{
				exit('编辑项不存在！');
			}
		}else{
			exit('编辑项不存在！');
		}
	}

	// 更新数据
	public function update() {
		$Dict	=	D("Dict");
		if($vo = $Dict->create()) {
			$list=$Dict->relation(false)->save();
			if($list!==false){
				$this->success('数据更新成功！');
			}else{
				$this->error("更新失败!");
			}
		}else{
			$this->error($Form->getError());
		}
	}
	// 删除数据
	public function delete() {
		if(!empty($_POST['id'])) {
			$Form	=	D("Dict");
			$result	=	$Form->relation(true)->delete($_POST['id']);
			if(false !== $result) {
				$this->ajaxReturn($_POST['id'],'删除成功！',1);
			}else{
				$this->error('删除出错！');
			}
		}else{
			$this->error('删除项不存在！');
		}
	}

	public function map_list(){
		if(!empty($_GET['dict_id'])) {
			$Form	=	D("Dict");
			$vo	= $Form->relation(true)->find($_GET['dict_id']);
			if($vo) {
				$this->assign('vo',$vo);
				$this->assign("dict_id",$_GET["dict_id"]);
				$this->assign("DictMap",$vo["DictMap"]);
				$this->display();
			}else{
				exit('编辑项不存在！');
			}
		}else{
			exit('编辑项不存在！');
		}
		$this->display();
	}
	// 删除数据
	public function map_delete() {
		if(!empty($_POST['id'])) {
			$Form	=	D("DictMap");
			$result	=	$Form->delete($_POST['id']);
			if(false !== $result) {
				$this->ajaxReturn($_POST['id'],'删除成功！',1);
			}else{
				$this->error('删除出错！');
			}
		}else{
			$this->error('删除项不存在！');
		}
	}

	public function map_add(){
		$Dict = M("DictMap");
		$parents = $Dict->findAll();
		$this->assign("dict_id",$_GET["dict_id"]);
		$this->assign("parents", $parents);
		$this-> display();
	}

	public function map_insert(){
		$Dict = D("DictMap");
		if($vo = $Dict->create()) {
			$list=$Dict->add();
			if($list!==false){
				$this->success('数据保存成功！');
			}else{
				$this->error('数据写入错误！');
			}
		}else{
			$this->error($Form->getError());
		}
	}

	// 编辑数据
	public function map_edit() {
		if(!empty($_GET['id'])) {
			$Form	=	D("DictMap");
			$vo	=	$Form->getById($_GET['id']);
			if($vo) {
				$this->assign("dict_id",$_GET["dict_id"]);
				$this->assign('vo',$vo);
				$this->display();
			}else{
				exit('编辑项不存在！');
			}
		}else{
			exit('编辑项不存在！');
		}
	}

	// 更新数据
	public function map_update() {
		$Dict	=	D("DictMap");
		if($vo = $Dict->create()) {
			$list=$Dict->save();
			if($list!==false){
				$this->success('数据更新成功！');
			}else{
				$this->error("更新失败!");
			}
		}else{
			$this->error($Form->getError());
		}
	}
}
?>